package ysf.com.mediumdifficulty;

/**
 * @author ysf
 * @date 2021/6/17 22:53
 */
public class EighthDemos {

    public static void main(String[] args) {
        String str = "-12568998855531ab789";
        int result = StringtoInt(str);
        System.out.println(result);

    }

    public static int StringtoInt(String str){

        StringBuilder stringBuilder = new StringBuilder();
        int count = 0;
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char c = str.charAt(i);
            if(c == '-' && count == 0){
                stringBuilder.append(c);
                count++;
            }
            // 判断字符c是否是数字字符 如 '1' , '2'  是则返回true 否则返回false
            /*boolean flag = Character.isDigit(c);*/
            if (Character.isDigit(c)){
                stringBuilder.append(c);
            }
            /*if(!Character.isDigit(c) && c != '-'){
                return Integer.parseInt(stringBuilder.toString());
            }*/
            //System.out.println("c is=====>"+c+"\tflag is =======>"+flag);
        }
        // int parseIntResult = Integer.parseInt(stringBuilder.toString());
        long parseLong = Long.parseLong(stringBuilder.toString());
        if(parseLong > Integer.MAX_VALUE){
            return Integer.MAX_VALUE;
        }
        if(parseLong < Integer.MAX_VALUE){
            return Integer.MIN_VALUE;
        }
        return (int) parseLong;
    }




}
